package com.ruoyi.web.controller.client;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.client.TransitRecords;
import com.ruoyi.system.service.ITransitRecordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 过站信息Controller
 *
 * @author ruoyi
 * @date 2023-02-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@Api(tags = "过站信息")
@RequestMapping("/transit/records" )
public class TransitRecordsController extends BaseController {

    private final ITransitRecordsService transitRecordsService;

    /**
     * 查询过站信息列表
     */
    @ApiOperation(value = "查询过站信息列表")
    @GetMapping("/list")
    public TableDataInfo list(TransitRecords transitRecords)
    {
        startPage();
        LambdaQueryWrapper<TransitRecords> lqw = new LambdaQueryWrapper<TransitRecords>();
        if (StringUtils.isNotBlank(transitRecords.getAssemblyCode())){
            lqw.like(TransitRecords::getAssemblyCode ,transitRecords.getAssemblyCode());
        }
        if (StringUtils.isNotBlank(transitRecords.getStationCode())){
            lqw.like(TransitRecords::getStationCode ,transitRecords.getStationCode());
        }
        if (StringUtils.isNotBlank(transitRecords.getSerialNumber())){
            lqw.eq(TransitRecords::getSerialNumber ,transitRecords.getSerialNumber());
        }

        List<TransitRecords> list = transitRecordsService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出过站信息列表
     */
    @Log(title = "过站信息" , businessType = BusinessType.EXPORT)
    @ApiOperation(value = "导出过站信息列表")
    @GetMapping("/export" )
    public AjaxResult export(TransitRecords transitRecords) {
        LambdaQueryWrapper<TransitRecords> lqw = new LambdaQueryWrapper<TransitRecords>(transitRecords);
        List<TransitRecords> list = transitRecordsService.list(lqw);
        ExcelUtil<TransitRecords> util = new ExcelUtil<TransitRecords>(TransitRecords. class);
        return util.exportExcel(list, "过站信息" );
    }

    /**
     * 获取过站信息详细信息
     */
    @ApiOperation(value = "获取过站信息详细信息")
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(transitRecordsService.getById(id));
    }

    /**
     * 新增过站信息
     */
    @Log(title = "过站信息" , businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增过站信息")
    @PostMapping
    public AjaxResult add(@RequestBody TransitRecords transitRecords) {
        return toAjax(transitRecordsService.save(transitRecords) ? 1 : 0);
    }

    /**
     * 修改过站信息
     */
    @Log(title = "过站信息" , businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改过站信息")
    @PutMapping
    public AjaxResult edit(@RequestBody TransitRecords transitRecords) {
        return toAjax(transitRecordsService.updateById(transitRecords) ? 1 : 0);
    }

    /**
     * 删除过站信息
     */
    @Log(title = "过站信息" , businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除过站信息")
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(transitRecordsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
